﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class FrontEnd_Control_Header
    Inherits System.Web.UI.UserControl
    Public URL As String = ""
    Dim level As String = 0
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



        If Not Page.IsPostBack Then
            Using data As New VSEntities
                hidMenuId.Value = Page.RouteData.Values("MenuId")
                If hidMenuId.Value = "" Then
                    hidMenuId.Value = 0
                End If
                Dim _lang As Integer = 1
                If Not Session("LanguageId") Is Nothing Then
                    _lang = Session("LanguageId")
                End If

                'xu ly ngon ngu
                If _lang = 2 Then
                    btnSearh.Text = "Seach"
                    txtSearch.Attributes.Add("placeholder", "keyword")
                End If

                Dim query = (From q In data.Menus Where q.ParentId = 0 AndAlso q.MenuPosition = 1 And q.LanguageId = _lang Order By q.Sort Select q).ToList

                With rptMenu
                    .DataSource = query
                    .DataBind()
                End With
            End Using
        End If


    End Sub
    Protected Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound


        Dim row As VSModel.Menu = CType(e.Item.DataItem, VSModel.Menu)
        Dim hplMenu As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Using data As New VSEntities
            If (Not row.NavigateUrl Is Nothing AndAlso row.NavigateUrl.ToString <> "") Then
                hplMenu.NavigateUrl = MainDomain & row.NavigateUrl
            Else
                hplMenu.NavigateUrl = MainDomain & row.QueryString & "-" & row.MenuId & ".html"
            End If
            Dim iMenuId As Integer = row.MenuId
            If iMenuId = hidMenuId.Value Then
                hplMenu.CssClass = "MenuActive"
            ElseIf iMenuId = (From q In data.Menus Where q.MenuId = hidMenuId.Value Select q.ParentId).FirstOrDefault Then
                hplMenu.CssClass = "MenuActive"
            End If

            Dim lblTitle As Label = CType(e.Item.FindControl("lblTitle"), Label)
            lblTitle.Text = row.Title.ToString

            'bind child menu
            Dim item As RepeaterItem = e.Item
            If item.ItemType = ListItemType.Item Or item.ItemType = ListItemType.AlternatingItem Then
                Dim rptChild = CType(item.FindControl("rptMenuChild"), Repeater)

                Dim fn = (From q In data.Menus Where q.ParentId = row.MenuId).ToList
                With rptChild
                    .DataSource = fn
                    .DataBind()
                End With

            End If
            'If hidRootCat.Value.ToString.Contains(row.CatId) Then
            '    hplTitle.CssClass = "active_parent"
            'End If
        End Using
    End Sub

    
    Protected Sub btnSearh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearh.Click
        Response.Redirect(MainDomain & "tim-kiem.html?s=" & txtSearch.Text)
    End Sub
    Protected Sub rptMenuChild_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

        Dim row As VSModel.Menu = CType(e.Item.DataItem, VSModel.Menu)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.MenuId.ToString & ".html"
        hplTitle.Text = row.Title.ToString


    End Sub

    Protected Sub lbtTiengAnh_Click(sender As Object, e As System.EventArgs) Handles lbtTiengAnh.Click
        Session("LanguageId") = 2
        Response.Redirect(MainDomain)
    End Sub

    Protected Sub lbtTiengViet_Click(sender As Object, e As System.EventArgs) Handles lbtTiengViet.Click
        Session("LanguageId") = 1
        Response.Redirect(MainDomain)
    End Sub
End Class
